library(tidyverse)
library(ggplot2)
library(ggthemes)
library(gridExtra)
library(RColorBrewer)
library(plotly)
salary <- read_csv("/Users/Dan/Research/TidyTuesday/tidy_tuesday_week2.csv")
gath <- salary %>%
  gather( pos, salary, -year) %>%
  group_by(pos, year) %>%
  top_n(16, salary) %>%
  filter(salary < 30000000) %>%
  mutate(salary=salary/1000000) 

# Assigning Positions to Offense or D/ST
gath$side <- ifelse(gath$pos == "Quarterback" | gath$pos == "Running Back" | gath$pos == "Wide Receiver" | gath$pos == "Tight End" | gath$pos == "Offensive Lineman" ,"Offense", "D/ST")
gath <- na.omit(gath)


# Expanding Sprectral Color Palette
coul = brewer.pal(8,"Spectral") 
coul = colorRampPalette(coul)(10)
coul <- sample(coul)


# Theme Customizing
facetSettings <-
  theme(panel.grid.major = element_line("lightgray",0.5),
        panel.grid.minor = element_line("lightgray",0.25))


k <- ggplot(gath, aes(x=year, y=salary, color = pos)) + 
  geom_smooth(method = "loess", se=F,  size = 3, span = 0.3) +
  geom_jitter(alpha=0.1, size = 4, width=0.15) +
  theme_minimal() +
  scale_colour_manual(values = coul) +
  ylab("Cap Hit ($ million)") +
  xlab("Year") +
  theme(legend.position="bottom") +
  geom_hline(yintercept=0)
k <- k + facet_wrap(~ side, ncol=5) + facetSettings
ggplotly(k) %>%
  layout(legend = list(orientation = "v"))